package cn.dujc.superkid.activities;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.MeExamMessageAdapter;
import cn.dujc.superkid.adapter.MyVideoMesssgeAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.MyTestMessageResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.configs.Globals;
import cn.dujc.superkid.utils.Preferences;

public class MeExamMessage extends BaseListActivity {
    private MeExamMessageAdapter meExamMessageAdapter;
    private List<MyTestMessageResult.MyTestMessageEntity> List = new ArrayList<>();
    private int mPage = 0;
    @Override
    public void initBasic(Bundle savedInstanceState) {
        super.initBasic(savedInstanceState);
        setTitle("我的考试留言");
        longClick();
        getSwipeRefreshLayout().setEnabled(false);//禁止下拉刷新
    }
    protected void recyclerViewOtherSetup() {
        if (mRvList != null) {
            //去掉下划线
//            mRvList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//注释掉此方法去掉下划线
//            myQuestionAdapter.disableLoadMoreIfNotFullPage();//禁止加载
        }
    }

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        meExamMessageAdapter = new MeExamMessageAdapter(List);
        return meExamMessageAdapter;
    }

    @Override
    protected void onItemClick(int position) {

    }

    @Override
    protected void loadMore() {
        loadData();
    }

    @Override
    protected void reload() {
        showRefreshing();
        mPage = 0;
        loadData();
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberMyTestMessage(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("num",Globals.PAGE_SIZE)
                .put("page", mPage)
                .create()).enqueue(new ResultCallBack<Result<MyTestMessageResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<MyTestMessageResult> body) {
                if (mPage <= 0) List.clear();
                boolean done = false;
                if (body != null){
                    final MyTestMessageResult data = body.getData();
                    if (data != null) {
                        final List<MyTestMessageResult.MyTestMessageEntity> list = data.getList();
                        if(List.size()==0){
                            meExamMessageAdapter.setEmptyView(R.layout.layout_empty_img);
                        }
                        if (list != null) {
                            List.addAll(list);
                        }
                        done = mPage >= data.getPage_num();
                    }
                }
                Handler handler = new Handler();
                final boolean finalDone = done;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged(finalDone);
                        mPage++;
                    }
                }, 500);
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                loadFailure();
                loadDone(true,false);
                meExamMessageAdapter.setEmptyView(R.layout.layout_empty_img);
            }
        });

    }

    private void longClick() {
        meExamMessageAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MeExamMessage.this);
                builder.setTitle("警告：")//设置标题
                        .setMessage("确定要删除吗？")//设置消息
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                deleteData(meExamMessageAdapter.getItem(position).getId(),position);
                            }
                        })
                        .setNegativeButton("取消", null)//取消按钮：点击事件为null，点击取消时，对话框就自动关闭
                        .show();
                return false;
            }
        });
    }

    private void deleteData(String getId, final int position) {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
//        public Call<Result<Object>> memberDelMessage(@FieldMap QMap map);
        HttpHelper.getUserCenter().memberDelTestMess(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("message_id", getId)
                .create()).enqueue(new ResultCallBack<Result<Object>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<Object> body) {
                ToastUtil.showToast(mActivity,"已删除！");
                meExamMessageAdapter.remove(position);
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
            }
        });
    }

}
